package com.nbcb.nwql.common.excel;

import com.alibaba.excel.EasyExcel;
import com.nbcb.nwql.applicantion.domain.MyExcel;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;

public class ExportUtil {

        public void test() throws Exception {
            String exprtFileNm = "C:\\Users\\40249\\Desktop\\export\\exp\\image.xlsx";
            // 图片位置
            String imagePath = "C:\\Program Files (x86)\\Steam\\steamapps\\workshop\\content\\431960\\819750546\\lks.jpg";
            // 网络图片
            //URL url = new URL("https://cn.bing.com/th?id=OHR.TanzaniaBeeEater_ZH-CN3246625733_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp");
//C:\Program Files (x86)\Steam\steamapps\workshop\content\431960\1781466079\preview.gif
            ArrayList<String> list = new ArrayList<>();
            ArrayList<URL> urls = new ArrayList<>();
            ArrayList<MyExcel> excelList = new ArrayList<>();

            for (int i = 0; i < 3; i++) {
                list.add(imagePath);
                urls.add(new URL("https://cn.bing.com/th?id=OHR.TanzaniaBeeEater_ZH-CN3246625733_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp"));
            }
            excelList.add(
                    new MyExcel().setImageUrls(urls.subList(0, 3)).setImageStrings(list.subList(0, 3)).setId("id1").setNum("1"));
            excelList.add(
                    new MyExcel().setImageUrls(urls.subList(0, 2)).setImageStrings(list.subList(0, 2)).setId("id2").setNum("2"));
            //异常string和url类型图片地址
            excelList.add(
                    new MyExcel()
.setImageUrls(
Arrays.asList(new URL("https://cn.bing.com/th?id=OHR.TanzaniaBeeEater_ZH-CN3246625733_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp")))
.setImageStrings(
Arrays.asList("C:\\Program Files (x86)\\Steam\\steamapps\\workshop\\content\\431960\\1781466079\\preview.gif")).setId("id3").setNum("3")
                    );
            //无图片
            excelList.add(
                    new MyExcel().setImageUrls(urls.subList(0, 1)).setImageStrings(list.subList(0, 1)).setId("id4").setNum("4"));

            //该文件导出到本地
            EasyExcel.write(exprtFileNm, MyExcel.class)
                    .registerWriteHandler(new CustomImageModifyHandler())
                    .sheet("pictrueExport")
                    .doWrite(excelList);

        }
    }